package LogisticsSystem.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import LogisticsSystem.po.Cargo;
import LogisticsSystem.po.Dispatch;
import LogisticsSystem.po.Orders;
import LogisticsSystem.po.Staff;
import LogisticsSystem.po.ext.OrderExtend;
import LogisticsSystem.service.CargoService;
import LogisticsSystem.service.DispatcherService;
import LogisticsSystem.service.OrderService;
import LogisticsSystem.service.StaffService;
import LogisticsSystem.service.impl.DispatchServiceImpl;

@Controller
@RequestMapping("cargo")
public class CargoController {
	@Autowired
	private CargoService cargoService;
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private StaffService staffService;
	
	@Autowired
	private DispatcherService dispatcherService;
	
	@RequestMapping("/input") //进货
	public ModelAndView inputCargo(String cargoName, double cargoCount, double cargoPrice){//进货
		System.out.println(cargoName);
		Cargo cargo = new Cargo();
		cargo.setCargoName(cargoName);
		cargo.setCargoPrice(cargoPrice);
		
		//查询商品是否已经存在
		List<Cargo> cargos =  cargoService.selectCargoByName(cargoName);
		if(cargos.size() > 0){//商品已经存在
			double oldCount = cargos.get(0).getCargoCount();
			double newCount = oldCount + cargoCount;
			cargo.setCargoCount(newCount);
			
			cargoService.updateCargo(cargo);
		}else{//商品不存在,即第一次进这种商品
			cargo.setCargoCount(cargoCount);
			cargoService.insertCargos(cargo);
		}
		
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("msg", "进货成功");
		mav.setViewName("inputCargo");//跳转回进货页面
		return mav;	
	}
	
	@RequestMapping("/forwardIC") //跳转到进货页面
	public ModelAndView forwardInputCargo(){
		ModelAndView mav = new ModelAndView();
		mav.setViewName("inputCargo");
		return mav;
	}
	
	@RequestMapping("/searchInventory") //库存页面
	public ModelAndView searchInventory(){
		List<Cargo> cargos = cargoService.getCargolist(null); //查询所有
		ModelAndView mav = new ModelAndView();
		mav.addObject("cs", cargos);
		mav.setViewName("inventory");
		return mav;
	}
	
	/*@RequestMapping("/forwardInventory") //跳转到库存页面
	public ModelAndView forwardInventory(){
		ModelAndView mav = new ModelAndView();
		mav.setViewName("inventory");
		return mav;
	}*/
	
	@RequestMapping("/findCargo") //通过商品名，进行商品查询(支持模糊查找)
	public ModelAndView findCargo(String cargoName){
		System.out.println(cargoName);
		//List<Cargo>  cargos = cargoService.selectCargoByName(cargoName);
		List<Cargo>  cargos = cargoService.selectCargoLikeName(cargoName);
		ModelAndView mav = new ModelAndView();
		mav.addObject("cs", cargos);
		mav.setViewName("inventory");
		return mav;
	}
	
	@RequestMapping("/dispatherTosub")//分配订单到子库存
	public ModelAndView dispatherToSubInventory() {
		List<OrderExtend> orderExtendList = orderService.selectOrderExtendList0();//查询未分配的订单
		List<Staff> subInv = staffService.selectAllDispatchByRole2("2"); //查询所有的子库存
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("orderList", orderExtendList);
		mav.addObject("subInv", subInv);
		mav.setViewName("serviceStaff");
		return mav;
	}
	
	@RequestMapping("/createDispatherForm")
	public ModelAndView createDispatherForm(Integer oId, Integer sId) {
		System.out.println(oId + "...."  + sId);
		Dispatch dispatch = new Dispatch(); 
		dispatch.setdId(null);
		dispatch.setoId(oId);
		dispatch.setsId(sId);
		dispatch.setRecTime(new Date());
		dispatcherService.insertDispatchs(dispatch); //生成派送单
		
		Orders orders = orderService.selectById(oId);//取出订单
		orders.setoStatus(101); //修改订单状态
		orderService.updateOrders(orders); //更新订单
		
		
		return new ModelAndView("redirect:/cargo/dispatherTosub", null);//跳转到 dispatherTosub这个Controller里面
		
	}
}
